/*增加URC回调组*/
/* <URC>:Unsolicited Result Code，即"非请求结果码" */
#include "t_at_urc_etgp.h"
#include "t_at_ringbuffer.h"
#include "t_at.h"
#include "t_platform.h"


rbuf urc_rbuf; // URC环形缓冲区
// urc使用

uint8_t cb_at_test(void *Ptx, uint8_t *dataBuf, uint8_t len, uint8_t *cmd_UrcString) {
    printf("cb_at_test: %s,%d\r\n", dataBuf, len);
    return 1;
}
extern uint8_t cb_mqtt_rev(void *Ptx, uint8_t *dataBuf, uint8_t len, uint8_t *cmd_UrcString);

static const rev_urc_etgp_t urc_table[] = 
{
 EXT_ETGP_CMD("+MHTTPURC:", cb_mqtt_rev),
 EXT_ETGP_CMD("hello", cb_at_test)
};

static const rev_urc_etgp_t *find_command(const rev_urc_etgp_t *cmd_table, const char *name) {
    for (size_t i = 0; i < sizeof(urc_table) / sizeof(rev_urc_etgp_t); i++) {
        if (strstr(name, cmd_table[i].name_key_word) != NULL) {
            return &cmd_table[i];
        }
    }
    return T_NULL;
}

// 如果这里直接在中断里会让栈耗尽，从而报错
uint8_t urc_handle_command_callback(void *arg,const char *cmd_buffer, uint16_t data_len) {
    uint8_t ret = T_FALSE;
    //printf("cmd_urc:%s,%d\r\n", cmd_buffer, data_len);
    const rev_urc_etgp_t *entry = find_command(urc_table, cmd_buffer);
    if (entry && entry->callback_func) {
        if (entry->callback_func(arg, (uint8_t *)cmd_buffer, data_len, NULL) ==1) {
            ret = T_TRUE;
        } else {
            ret = T_FALSE;
        }
    } else {
        ret = T_FALSE;
    }
    return ret;
}
static uint8_t urc_lock_push(const rev_urc_etgp_t *cmd_table, uint8_t *buffer) {
    uint8_t ret = 0;
    for (size_t i = 0; i < sizeof(urc_table) / sizeof(rev_urc_etgp_t); i++) {
        if (strstr((char*)buffer, cmd_table[i].name_key_word) != NULL) {
            ret= 1;
					  return ret;
        }
    }
		return ret;
}


//压入数据()未带lock 任何返回值都会被压入实现一个lock
void urc_thread_push_ISR(t_at_t *me, uint8_t *buffer, uint16_t len) {
    if (me == T_NULL || me->urc_rbufer == T_NULL || buffer == T_NULL || len == 0) {
        return;
    }
    rdata_buff urc_data;
    //检查（）
    if (urc_lock_push(urc_table, buffer) == 0) {//拦截其他指令

        return; // URC未找到
    }
    memset(&urc_data, 0, sizeof(rdata_buff));
    urc_data.buf_len = (len > URC_DATA_BUFFER_SIZE) ? URC_DATA_BUFFER_SIZE : len;
    memcpy(urc_data.buffer, buffer, urc_data.buf_len);
    
    if (me->rbuf_push(me->urc_rbufer, &urc_data) != RBUF_OK) {
        printf("URC buffer push failed\n");
    }
}

// no-rtos：这一步在中断中操作可以开一个1MS定时器去刷
//直接调用urc_handle_command_callback//
void urc_handle_event_no_RTOS(t_at_t *at_device_t)
{
    //检查是否挂载了URC环形缓冲区
    if (at_device_t == T_NULL || at_device_t->urc_rbufer== T_NULL) {
        return;
    }
    rdata_buff urc_payload;
    // 处理URC事件
    if(at_device_t->rbuf_is_empty(at_device_t->urc_rbufer) != RBUF_EMPTY) {
        at_device_t->rbuf_pop(at_device_t->urc_rbufer, &urc_payload);
        if (urc_payload.buffer[0] != '\0') {
            urc_handle_command_callback(NULL,(const char *)urc_payload.buffer, urc_payload.buf_len);
        }
    }
}
//// 处理URC事件（创建一个线程回调函数）
void urc_handle_event(void *at_device_t) {
    t_at_t *me = (t_at_t *)at_device_t;
    if (me == T_NULL || me->urc_rbufer== T_NULL) {
        return;
    }
    rdata_buff urc_payload;

    while (1) {
        if (me->rbuf_is_empty(me->urc_rbufer) != RBUF_EMPTY) {
            me->rbuf_pop(me->urc_rbufer, &urc_payload);
            if (urc_payload.buffer[0] != '\0') {
                urc_handle_command_callback(me->urc_arg,(const char *)urc_payload.buffer, urc_payload.buf_len);
            }
        }
        // 休眠一段时间，避免CPU占用过高
        vTaskDelay(2);
    }
}
//这个函数之前就要确保 rbuf 对象挂载AT对象请使用挂载函数
void at_urc_thread_init(t_at_t * at_device_t)
{
  xTaskCreate(urc_handle_event,
              "at_urc_thread",
              (128*3),
              (void *)at_device_t,
              1,
              NULL);
}